package com.srp.tools;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

@Component
public class FederalLearningModel {
    @Value("${federalLearningModel.id3gini.pythonInterpreterPath}")
    private  String pythonInterpreterPath;
    @Value("${federalLearningModel.id3gini.ModelPath}")
    private  String id3giniModelPath;

    public void main(String[] args) throws IOException, InterruptedException {
        // TODO Auto-generated method stub
//        get_stock_price("2023-04-23", "2023-04-24");//       get_stock_enterprise();
//        get_stock_heat();
//        get_stock_realtime_data("SZ000010");
//        String[] data_array = get_stock_realtime_data("SZ000001");
//        id3gini("-M 5 -datasetPath D:/python/code/mpyc-master/demos/data/id3/car1.csv");

    }


    //使用指令操作构建决策树
//    public String id3gini(String code,String code2,String code3,String code4,String code5,String code6,String code7) throws IOException, InterruptedException {
    public String id3gini(String []code) throws IOException, InterruptedException {

        try {
            System.out.println("[Python] \"id3gini\":start");
            /*下面这行String里的数组很关键，它有两个参数，都推荐使用绝对路径，
            第一个参数是你python编译器所在的位置，直接写python，系统会使用默认的python，
            第二个参数是你python文件所在的位置。
            之后每个参数代表一个参数
            */
//            String[] arg = new String[]{pythonInterpreterPath, id3giniModelPath,code,code2,code3,code4,code5,code6,code7
//            };
            String[] pyArg = new String[]{pythonInterpreterPath, id3giniModelPath};
            // 确定新数组的大小
            int length = pyArg.length + code.length;

            // 创建新数组
            String[] arg = new String[length];

            // 使用 System.arraycopy() 复制第一个数组的元素
            System.arraycopy(pyArg, 0, arg, 0, pyArg.length);

            // 使用 System.arraycopy() 复制第二个数组的元素
            System.arraycopy(code, 0, arg, pyArg.length, code.length);

            Process pr = Runtime.getRuntime().exec(arg);
            //数据集必须是UTF-8
            BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream(),"UTF-8"));

            String context = "";
            String res="";
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println("[Python] " + line);
                line = new String(line.getBytes("UTF-8"),"UTF-8");
                context =context + line + '\n';

            }
            int startIndex = context.indexOf("Decision tree") ;
            String decisionTree = context.substring(startIndex).trim();

            System.out.println(decisionTree);
            res = decisionTree;
            in.close();

            // 结束
            System.out.println("[Python] \"id3gini\":end");

            pr.waitFor();
            InputStream errorStream = pr.getErrorStream();
            BufferedReader error = new BufferedReader(new InputStreamReader(errorStream, "gbk"));

            String lines;
            while ((lines = error.readLine()) != null) {
                System.out.println("[PythonError]" + lines);

            }
            error.close();

            return res;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "null";
    }

    //
    public static String[] id3giniTest() throws IOException, InterruptedException {
        try {
            System.out.println("[Python] \"id3gini\":start");
            /*下面这行String里的数组很关键，它有两个参数，都推荐使用绝对路径，
            第一个参数是你python编译器所在的位置，直接写python，系统会使用默认的python，
            第二个参数是你python文件所在的位置。
            */
            String[] arg = new String[]{"C:/Users/wxy/.conda/envs/MachineLearing/python.exe", "D:/python/code/mpyc-master/demos/id3gini.py",
                    "-M 5"};
            Process pr = Runtime.getRuntime().exec(arg);
            BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));

            String res = "null";

            String line;
            while ((line = in.readLine()) != null) {
                System.out.println("[Python] " + line);
                res = line;
            }
            in.close();

            // 结束
            System.out.println("[Python] \"id3gini\":start");

            pr.waitFor();
            InputStream errorStream = pr.getErrorStream();
            BufferedReader error = new BufferedReader(new InputStreamReader(errorStream, "gbk"));

            String lines;
            while ((lines = error.readLine()) != null) {
                System.out.println("[PythonError]" + lines);

            }
            error.close();

            return res.split(",");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new String[]{"null"};
    }

}



